<?php

/*
 * Copyright 2008 the original author or authors.
 * 
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 *      http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#namespace tehframework\objects\propertyeditors;

require_once 'test/testHelper.php';

class StringEditorTest extends BaseEditorTestCase
{
	protected function createEditor()
	{
		return new StringEditor();
	}
	
	function testCanEditPropertyReturnsTrueForAnyType()
	{
		$h = fopen(__FILE__, 'r');
		
		try
		{
			$this->assertTrue($this->e->canEditProperty($h));
		}
		catch (Exception $x)
		{
			fclose($h);
			
			throw $x;
		}
		
		$propertyValues = array(
			1, 1.1, 'foo', new stdClass, $this, true, false, null, array(1, 'two'));
		
		foreach ($propertyValues as $propertyValue)
		{
			$this->assertTrue($this->e->canEditProperty($propertyValue));
		}
	}
	
	function testEditPropertyReturnsSpecifiedValueIfItsTypeIsString()
	{
		$this->assertSame('foobar', $this->e->editProperty('foobar'));
	}
	
	function testEditPropertyReturnsResultOfToStringIfSpecifiedValueIsObjectWithToStringMethod()
	{
		$this->assertSame((string)$this, $this->e->editProperty($this));
	}
	
	function testEditPropertyReturnsClassNameIfSpecifiedValueIsObjectWithoutToStringMethod()
	{
		$this->assertSame('stdClass', $this->e->editProperty(new stdClass));
	}
	
	function testEditPropertyReturnsArrayWithNumberOfElementsStringIfSpecifiedValueIsArray()
	{
		$this->assertSame('array[0]', $this->e->editProperty(array()));
		$this->assertSame('array[3]', $this->e->editProperty(array(1, 2, 3)));
	}
	
	function testEditPropertyReturnsNULLStringIfSpecifiedValueIsNull()
	{
		$this->assertSame('NULL', $this->e->editProperty(null));
	}
	
	function testEditPropertyReturnsTRUEStringIfSpecifiedValueIsBooleanTrue()
	{
		$this->assertSame('TRUE', $this->e->editProperty(true));
	}
	
	function testEditPropertyReturnsFALSEStringIfSpecifiedValueIsBooleanFalse()
	{
		$this->assertSame('FALSE', $this->e->editProperty(false));
	}
	
	function testEditPropertyReturnsResourceTypeStringIfSpecifiedValueIsResource()
	{
		$h = fopen(__FILE__, 'r');
		
		try
		{
			$this->assertSame('stream', $this->e->editProperty($h));
		}
		catch (Exception $x)
		{
			fclose($h);
			
			throw $x;
		}
	}
	
	function testEditPropertyCastsIntegersToStrings()
	{
		$this->assertSame('-1', $this->e->editProperty(-1));
		$this->assertSame('0', $this->e->editProperty(0));
		$this->assertSame('1', $this->e->editProperty(1));
	}
	
	function testEditPropertyCastsFloatsToStrings()
	{
		$this->assertSame('-1.1', $this->e->editProperty(-1.1));
		$this->assertSame('0', $this->e->editProperty((float)0));
		$this->assertSame('1.1', $this->e->editProperty(1.1));
	}
	
	public function __toString()
	{
		return 'foobar';
	}
}